<?php

namespace app\admin\controller\courier;

use app\common\controller\Backend;
use think\Db;
/**
 * 商城订单
 *
 * @icon fa fa-circle-o
 */
class Details extends Backend
{
    
    /**
     * Details模型对象
     * @var \app\admin\model\courier\Details
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\courier\Details;
        $this->view->assign("paymentList", $this->model->getPaymentList());
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
    /****
     * Class Index          快递员完成订单详细
     * @$ids[int]           快递员id
     * @$uid[int]           快递员对应用户id
     * @list[array]         全部查询
     * @remark              其他按原框架FastAdminstatus开发
     * @date                2020/05/11 
     * @package application\admin\controller\courier
    */
    public function index()
    {   $ids = input('ids');
        $count = Db::name('order')->where(['courier_id'=>$ids])->count();
        $sum = Db::name('order')->where(['courier_id'=>$ids])->sum('pack');
        // dump($sum);die;
        $this->relationSearch = false;
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(); 
            $uid = input('u_id');
            $mywhere = [];
            $mywhere["user_id"] = $uid;
            $total = $this->model
                    ->with(['manystore','user'])
                    ->where($mywhere)
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['manystore','user'])
                    ->where($mywhere)
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $row->visible(['id','mid','total_price','pack','coupon_id','payment','type','shipping_fee','orderid','order_time','finish_time','status','sort','site_id','user_id','manystore','user']);
                
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        $this->view->assign("ids", $ids);
        $this->view->assign("count", $count);
        $this->view->assign("sum", $sum);
        return $this->view->fetch();
    }
}
